var express = require('express');
var router = express.Router();
var retUtil = require('./util/retUtil');
var CODE = require('../../../common/code');
var CONFIG = require('../../../common/config');
var documentUtil = require('./util/documentUtil');
var orderUtil = require('./util/orderUtil');
var userUtil = require('./util/userUtil');
var sundryUtil = require('./util/sundryUtil');
var staticizeUtil = require('./util/staticizeUtil');


router.get('/',function (req, res, next) {
    res.send('respond with a sundry resource');
});


/**
 * 获取 APP 基础信息
 *
 * @author  王俊
 * @date 2017-11-15
 *
 * device	是
 *               610
 *               6 字段为 一键秀 相关标记
 *               600 一键秀网页端
 *               610 一键秀 android 端
 *               620 一键秀 iOS 端
 *               630 一键秀 嵌入网页端
 *               640 一键秀微信端
 *               650 一键秀小程序端
 *
 * 返回: 参见接口: http://114.55.172.253:7000/project/11/interface/api/11
 *      types
 *      fontList
 *      colorList
 *
 * 测试地址: http://localhost:3000/yjx/v1/sundry/getAppBaseInfo_v1_0
 * */
function getAppBaseInfo_v1_0(req, res, next) {

    var device = retUtil.numberFromReq(req, 'device');

    // 如果有缓存, 优先使用缓存
    var uri = '/yjx/v1/sundry/getAppBaseInfo_v1_0';
    var para = {
        device : device
    };
    staticizeUtil.getCacheDataByRedis(uri, para, function (error, content) {
        if(content && CONFIG.DEBUG !== 1) {
            return retUtil.ret_succ_json(content, res);
        }
        sundryUtil.getAppBaseInfo_v1_0(device, function (error, result) {
            if(error) {
                console.log('yjx/v1/sundry.js [getAppBaseInfo_v1_0] error' + error);
                return retUtil.ret_err_json(error, res);
            }
            // 缓存至 redis
            staticizeUtil.recordDataByRedis(uri, para, result);
            return retUtil.ret_succ_json(result, res);
        });
    });
}
router.get('/getAppBaseInfo_v1_0', getAppBaseInfo_v1_0);
router.post('/getAppBaseInfo_v1_0', getAppBaseInfo_v1_0);

/**
 * 获取 OSS 上传文件签名
 *
 * @author 王俊
 * @date 2017-11-15
 *
 * device  6 字段为 一键秀 相关标记
 *          600 一键秀网页端
 *          610 一键秀 android 端
 *          620 一键秀 iOS 端
 *          630 一键秀 嵌入网页端
 *          640 一键秀微信端
 *          650 一键秀小程序端
 * strToSign	要签名的字符串, 由 Oss sdk 提供, 有空格换行符等, 注意编码
 *
 * 测试地址: http://localhost:3000/yjx/v1/sundry/getOssSign_v1_0?device=610&strToSign=hhhhhhhh
 */
function getOssSign_v1_0(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var strToSign = retUtil.stringFromReq(req, 'strToSign');

    if (!strToSign || strToSign.length <=0) {
        retUtil.ret_err_json('参数错误', res);
    }
    sundryUtil.getOssSign_v1_0(strToSign,function (signErr, data) {
        if (signErr) {
            console.log('yjx/v1/sundry.js [sundryUtil.getOssSign_v1_0] error' + signErr);
            return retUtil.ret_err_json(signErr, data);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getOssSign_v1_0', getOssSign_v1_0);
router.post('/getOssSign_v1_0', getOssSign_v1_0);

/**
 * 获取搜索热词
 *
 * @author 严旭东
 * @date 2017-11-15
 *
 *
 *device:  6字段为 一键秀 相关标记
 *         600 一键秀网页端
 *         610 一键秀 android 端
 *         620 一键秀 iOS 端
 *         630 一键秀 嵌入网页端
 *         640 一键秀微信端
 *         650 一键秀小程序端
 *
 * 返回值：{"success":1,"msg":"成功","data":{"list":["美食","人民的名义","食品","促销","节气","教育","培训"]}}
 * 测试地址：http://localhost:3000/yjx/v1/sundry/getHotSearch_v1_0?device=610
 * */

function getHotSearch_v1_0 (req,res,next){
    var device = retUtil.numberFromReq(req,'device');

    // 如果有缓存则优先使用缓存
    var uri = '/yjx/v1/sundry/getHotSearch_v1_0';
    var para = {
        device : device
    };
    staticizeUtil.getCacheDataByRedis(uri, para, function (error, content) {
        if(content) {
            return retUtil.ret_succ_json({list:content},res);
        }
        sundryUtil.getHotSearch_v1_0(function(err,list){
            if(err) {
                return retUtil.ret_err_json(err, res);
            }
            staticizeUtil.recordDataByRedis(uri, para, list);
            return retUtil.ret_succ_json({list:list},res);
        })
    });
}
router.get('/getHotSearch_v1_0', getHotSearch_v1_0);
router.post('/getHotSearch_v1_0', getHotSearch_v1_0);


/***
 * 获取 app 开屏广告信息
 * @author 李强
 * @date 2017-11-16
 * 参数 screenW 需要的宽
 * 参数 screenH 需要的高
 * 参数 appVer  版本号
 * 参数 callback 回调
 * 参数 device:  6字段为 一键秀 相关标记
 *         600 一键秀网页端
 *         610 一键秀 android 端
 *         620 一键秀 iOS 端
 *         630 一键秀 嵌入网页端
 *         640 一键秀微信端
 *         650 一键秀小程序端
 *返回值 : http://114.55.172.253:7000/mock/11/yjx/v1/sundry/getAppAdInfo_v1_0
 * 测试地址 :http://localhost:3000/yjx/v1/sundry/getAppAdInfo_v1_0?device=610&screenW=750&screenH=1334&appVer=1
 */
function getAppAdInfo_v1_0 (req,res,next){
    var device = retUtil.numberFromReq(req,'device');
    var screenW = retUtil.numberFromReq(req,'screenW');
    var screenH = retUtil.numberFromReq(req,'screenH');
    var appVer = retUtil.numberFromReq(req,'appVer');
    if (screenW<=0 || screenH<=0 || appVer <= 0) {
        return retUtil.ret_err_json('参数错误', res);
    }
    var uri = '/yjx/v1/sundry/getAppAdInfo_v1_0';
    var para = {
        device:device,
        screenW: screenW,
        screenH: screenH,
        appVer : appVer
    };

    staticizeUtil.getCacheDataByRedis(uri, para, function(error, content){
        if(content && CONFIG.DEBUG != 1){ // 有缓存, 且不是本地调试, 直接返回
            return retUtil.ret_err_json(CODE.OPERATION_SUCCESS, res);
        }
        sundryUtil.getAppAdInfo_v1_0(screenW, screenH,appVer, function(error, info){
            if(error) {
                console.log('/yjx/v1/sundry.js [getAppAdInfo_v1_0] err: ' + error);
                return retUtil.ret_err_json(CODE.OPERATION_FAILED, res);
            }
            var content = staticizeUtil.recordDataByRedis(uri, para, info);  // 缓存
            return retUtil.ret_succ_json(info, res);
        });
    });
}
router.get('/getAppAdInfo_v1_0', getAppAdInfo_v1_0);
router.post('/getAppAdInfo_v1_0', getAppAdInfo_v1_0);

/***
 * 上传 APP 错误日志接口
 *
 * @author 毛凯
 * @date 2017-11-17
 *
 *         是否必须
 * device	是       610  6 字段为 一键秀 相关标记
 *                        600 一键秀网页端
 *                        610 一键秀 android 端
 *                        620 一键秀 iOS 端
 *                        630 一键秀 嵌入网页端
 *                        640 一键秀微信端
 *                        650 一键秀小程序端
 * deviceInfo 是     {                         用户设备基础信息
 *                    "appVersion":"6.2.1",
 *                    "deviceType":"DUK-AL20",
 *                    "systemVersion":"7.0"
 *                  }
 * errorLog	是      ""      错误日志
 * userId	否    299188   用户唯一标识
 * 返回结果参考: http://114.55.172.253:7000/project/11/interface/api/76
 * 测试地址 :http://localhost:3000/yjx/v1/sundry/uploadAppErrLog_v1_0?device=610&deviceInfo={"appVersion":"6.2.1","deviceType":"DUK-AL20","systemVersion":"7.0"}&errorLog=""&userId=123
 */
function uploadAppErrLog_v1_0 (req,res,next){
    var device = retUtil.numberFromReq(req,'device');
    var userId = retUtil.numberFromReq(req,'userId');
    var errorLog = retUtil.stringFromReq(req,'errorLog');
    var deviceInfo = retUtil.stringFromReq(req,'deviceInfo');
    if(!deviceInfo || device <= 0){
       return retUtil.ret_err_json('参数错误',res);
    }
    sundryUtil.uploadAppErrLog_v1_0(device,userId,deviceInfo,errorLog,function(err){
        if(err){
            console.log("sundry.js [uploadAppErrLog_v1_0] err" + err);
           return retUtil.ret_err_json(err,res);
        }
        return retUtil.ret_succ_json({},res);
    })
}
router.get('/uploadAppErrLog_v1_0', uploadAppErrLog_v1_0);
router.post('/uploadAppErrLog_v1_0', uploadAppErrLog_v1_0);


/***
 * 获取 yjx APP 最新版本下载地址
 *
 * @author 毛凯
 * @date 2017-11-20
 *
 *         是否必须
 * device	是       610  6 字段为 一键秀 相关标记
 *                        600 一键秀网页端
 *                        610 一键秀 android 端
 *                        620 一键秀 iOS 端
 *                        630 一键秀 嵌入网页端
 *                        640 一键秀微信端
 *                        650 一键秀小程序端
 * deviceInfo	是  设备信息结构的 json 字符串
 *                        {
 *                            "device": 610,
 *                            "channel" : "xiaomi",
 *                             "deviceType" : "mi 5",
 *                             "systemVersion" : "android 5.1.0",
 *                            "appVersion" : "5.6.0"
 *                         }
 * 返回结果参考: http://114.55.172.253:7000/project/11/interface/api/121
 * 测试地址 :http://localhost:3000/yjx/v1/sundry/getLastVersion_v1_0?device=600&deviceInfo={"device": 610,"channel" : "xiaomi","deviceType" : "mi 5","systemVersion" : "android 5.1.0","appVersion" : "5.6.0"}
 */
function getLastVersion_v1_0 (req,res,next){
    var device = retUtil.numberFromReq(req,'device');
    var deviceInfo = retUtil.stringFromReq(req,'deviceInfo');
    if(device <= 0){
        return retUtil.ret_err_json('参数错误',res);
    }
    sundryUtil.getLastVersion_v1_0(device,function(err,result){
        if(err) {
            console.log('users.js [getLastVersion_v5_7] getLastVersion_v5_7 err:' + err);
            return retUtil.ret_err_json(err, res);
        }
        // 查询到最新版本信息
        if(result) {
            var verInfo = {
                apkUrl : result.url,
                upgradeUrl : "http://yjxapphome.itbour.com/html/download.html",
                lastVersion : result.ver
            };
            return retUtil.ret_succ_json(verInfo, res);
        }
        // 查询不到
        return retUtil.ret_err_json("没有获取到最新版本", res);
    });
}
router.get('/getLastVersion_v1_0', getLastVersion_v1_0);
router.post('/getLastVersion_v1_0', getLastVersion_v1_0);


/**
 * 搜索模板
 *
 * @author 王俊
 * @date 2017-11-27
 *
 * 参数: device       6 字段为 一键秀 相关标记
 *                   600 一键秀网页端
 *                   610 一键秀 android 端
 *                   620 一键秀 iOS 端
 *                   630 一键秀 嵌入网页端
 *                   640 一键秀微信端
 *                   650 一键秀小程序端
 * 参数: showTypes    一级分类数组的 json 字符串, 如需全部则传递所有的一级分类 id, 若传递空数组的 json 字符串则认为参数异常
 * 参数: orderBy      排序方式, 暂时约定如下 1. 按时间排序   2. 按使用次数排序
 * 参数: pageNum      页码
 * 参数: pageSize     页容量
 * 参数: keyword      去表情换行等特殊字符, 字符数0-30, 若为空字符串则此条件失效, 默认为空字符串
 * 参数[可选]: setIds  指定模板组参数
 * 参数[可选]: withTotalPage   是否需要总页数
 * 参数[可选]: userId       用户唯一标识
 * 参数[可选]: tmplTypes 模板分类数组的 json 字符串, 0 代表普通模板, 10 代表 vip 模板, 不传默认为 [0], 标记只获取普通模板
 * 参数[可选]: durations 模板分类数组的 json 字符串, 1 代表0-10秒, 2 代表 11-30, 3代表 31-60   4代表61-90
 *
 * 测试地址: http://localhost:3000/yjx/v1/sundry/searchTemplete_v1_0?device=610&showTypes=[1,2,3,4,5]&setIds=[1,2,3,4,5]&orderBy=1&pageNum=0&pageSize=12&keyword=促销,劳动节
 * */
function searchTemplete_v1_0(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var showTypes = retUtil.arrayFromReq(req, 'showTypes');
    var orderBy = retUtil.numberFromReq(req, 'orderBy');
    var pageNum = retUtil.numberFromReq(req, 'pageNum');
    var pageSize = retUtil.numberFromReq(req, 'pageSize') || 12;
    var keyword = retUtil.stringFromReq(req, 'keyword');
    var withTotalPage = retUtil.numberFromReq(req, 'withTotalPage');
    var userId = retUtil.numberFromReq(req, 'userId');
    var setIds = retUtil.arrayFromReq(req, 'setIds') || [];
    var tmplTypes = retUtil.arrayFromReq(req, 'tmplTypes') || [0,10];
    var durations = retUtil.arrayFromReq(req, 'durations') || [1,2,3]; // 默认获取10s 模板, 避免老 app 使用较长模板
    var sizeIds = retUtil.arrayFromReq(req, 'sizeIds') || [90,95];
    if(!showTypes || showTypes.length <=0) {
        return retUtil.ret_err_json('showTypes 参数错误', res);
    }
    if(orderBy !== 1 && orderBy !== 2 && orderBy !== 3 &&  orderBy !== 4) {
        return retUtil.ret_err_json('orderBy 参数错误', res);
    }
    if(!keyword || keyword.length <=0 || keyword.length >=40) {
        return retUtil.ret_err_json('keyword 参数错误', res);
    }
    //过滤参数中的特殊字符
    var pattern = new RegExp("[`~!@#$^&*%()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
    var keywordRs = "";
    for(var i = 0; i < keyword.length; i++) {
        keywordRs += keyword.substr(i, 1).replace(pattern, '');
    }

    sundryUtil.searchUniItemsFromRedis(showTypes, keywordRs, setIds,orderBy, pageNum, pageSize,withTotalPage,userId, tmplTypes,durations,sizeIds,function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });

}
router.get('/searchTemplete_v1_0', searchTemplete_v1_0);
router.post('/searchTemplete_v1_0', searchTemplete_v1_0);


/**
 * 获取 APP banner 信息
 *
 * @author 王俊
 * @date 2018-01-05
 *
 * 参数: device       6 字段为 一键秀 相关标记
 *                   600 一键秀网页端
 *                   610 一键秀 android 端
 *                   620 一键秀 iOS 端
 *                   630 一键秀 嵌入网页端
 *                   640 一键秀微信端
 *                   650 一键秀小程序端
 *
 * 参数: appVer      APP 版本号
 *
 * */
function getAppBannerInfo_v1_1(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var appVer = retUtil.stringFromReq(req, 'appVer');

    sundryUtil.getAppBannerInfo_v1_1(appVer, function (error, data) {
        if (error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    })

}
router.get('/getAppBannerInfo_v1_1', getAppBannerInfo_v1_1);
router.post('/getAppBannerInfo_v1_1', getAppBannerInfo_v1_1);

/**
 * APP 启动激活
 *
 * @author 王俊
 * @date 2018-01-17
 *
 * 参数: appId            APP 在 AppStore 内的唯一标识
 * 参数: idfa             用户设备的广告标识
 * 参数: appVer           APP 当前版本
 * 参数: deviceInfo       APP 当前版本的设备基础信息的 json 字符串
 *
 * 接口文档地址: http://114.55.172.253:7000/project/11/interface/api/758
 * */
function appLaunchActivate(req, res, next) {
    var appId = retUtil.stringFromReq(req, 'appId');
    var idfa = retUtil.stringFromReq(req, 'idfa');
    var appVer = retUtil.stringFromReq(req, 'appVer');
    var deviceJson = retUtil.stringFromReq(req, 'deviceInfo');
    var deviceInfo = retUtil.safe_parser_JSON_string(deviceJson);

    if (!appId || appId.length <=0) {
        return retUtil.ret_err_json('appid 错误', res);
    }
    if (!idfa || idfa.length <=0 || idfa.indexOf('-') <=0  || idfa === "00000000-0000-0000-0000-000000000000") {
        return retUtil.ret_err_json('idfa 错误', res);
    }

    sundryUtil.appLaunchActivate(appId, idfa, appVer, deviceInfo, function (error) {
       if (error) {
           return retUtil.ret_err_json(error, res);
       }
       return retUtil.ret_succ_json({}, res);
    });

}
router.get('/appLaunchActivate', appLaunchActivate);
router.post('/appLaunchActivate', appLaunchActivate);



/**
 * 获取视频各分类首页数据_WEB_1_0
 *
 * @author 毛凯
 * @date 2018-1-24
 *
 * 参数: device       6 字段为 一键秀 相关标记
 *                   600 一键秀网页端
 *                   610 一键秀 android 端
 *                   620 一键秀 iOS 端
 *                   630 一键秀 嵌入网页端
 *                   640 一键秀微信端
 *                   650 一键秀小程序端
 * 参数: pageSize     页容量
 *
 * 测试地址: http://localhost:3000/yjx/v1/sundry/getVideoFirstPageForWeb_v1_0?device=610&pageSize=12
 * */
function getVideoFirstPageForWeb_v1_0(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var pageSize = retUtil.numberFromReq(req, 'pageSize');
    if(device <= 0 || pageSize<=0){
        return retUtil.ret_err_json("参数有误", res);
    }

    sundryUtil.getVideoFirstPageForWeb_v1_0(pageSize,function (error,data) {
        if (error) {
            console.log("sundry.js [getVideoFirstPageForWeb_v1_0] err:"+error);
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });

}
router.get('/getVideoFirstPageForWeb_v1_0', getVideoFirstPageForWeb_v1_0);
router.post('/getVideoFirstPageForWeb_v1_0', getVideoFirstPageForWeb_v1_0);

/**
 * 获取视频各分类首页数据_WEB_1_0
 *
 * @author 毛凯
 * @date 2018-2-1
 *
 * 参数: device       6 字段为 一键秀 相关标记
 *                   600 一键秀网页端
 *                   610 一键秀 android 端
 *                   620 一键秀 iOS 端
 *                   630 一键秀 嵌入网页端
 *                   640 一键秀微信端
 *                   650 一键秀小程序端
 *
 * 测试地址: http://localhost:3000/yjx/v1/sundry/getWebBannerInfo_v1_0?device=610
 * */
function getWebBannerInfo_v1_0(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    if(device <= 0){
        return retUtil.ret_err_json("参数有误", res);
    }
    sundryUtil.getWebBannerInfo_v1_0(function (error, data) {
        if (error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    })

}
router.get('/getWebBannerInfo_v1_0', getWebBannerInfo_v1_0);
router.post('/getWebBannerInfo_v1_0', getWebBannerInfo_v1_0);


/*
* 获取 APP 首页信息
*
* @author 王俊
* @date 2018-03-01
*
* 参数: device            6 字段为 一键秀 相关标记
*                        600 一键秀网页端
*                        610 一键秀 android 端
*                        620 一键秀 iOS 端
*                        630 一键秀 嵌入网页端
*                        640 一键秀微信端
*                        650 一键秀小程序端
* 参数: appVer            APP 版本号
*
*
* */
function getAppHomePageInfo_v1_3(req, res, next) {

    var device = retUtil.numberFromReq(req, 'device');
    var appVer = retUtil.stringFromReq(req, 'appVer');
    var actPageSize = retUtil.stringFromReq(req, 'actPageSize') || 6;

    var uri = '/yjx/v1/sundry/getAppAdInfo_v1_0';
    var para = {
        device:device,
        appVer: appVer,
        actPageSize : actPageSize
    };
    staticizeUtil.getCacheDataByRedis(uri, para, function(getCacheErr, content){
        if (!getCacheErr && content && CONFIG.DEBUG !== 1) {
            return retUtil.ret_succ_json(content, res);
        }

        sundryUtil.getAppHomePageInfo_v1_3(device, appVer,actPageSize, function (error, data) {
            if (error) {
                console.log('yjx/v1/sundry.js [getAppHomePageInfo_v1_3] err: ' + error);
                return retUtil.ret_err_json(error, res);
            }
            // 20180616 补丁start：
            // 将iOS的挑战活动金额除以100, 指定版本 2.0..0
            if(device && device === 620 && appVer === '2.0.0') {
                if(data && data.activitys && data.activitys.list && data.activitys.list.length>0){
                    for(var i=0; i<data.activitys.list.length; i++){
                        var one = data.activitys.list[i];
                        if(one && one.rewardTotal){
                            one.rewardTotal/=100;
                        }
                    }
                }
            }
            // 补丁end

            staticizeUtil.recordDataByRedis(uri, para, data);
            return retUtil.ret_succ_json(data, res);
        });
    });

}
router.get('/getAppHomePageInfo_v1_3', getAppHomePageInfo_v1_3);
router.post('/getAppHomePageInfo_v1_3', getAppHomePageInfo_v1_3);

/**
 * 获取 APP 基础信息1.3
 *
 * @author  毛凯
 * @date 2018-3-1
 *
 * device	是   610
 *               6 字段为 一键秀 相关标记
 *               600 一键秀网页端
 *               610 一键秀 android 端
 *               620 一键秀 iOS 端
 *               630 一键秀 嵌入网页端
 *               640 一键秀微信端
 *               650 一键秀小程序端
 * appVer  1.3.0 app版本号
 *
 * 返回: 参见接口: http://114.55.172.253:7000/project/11/interface/api/1202
 *      types
 *      fontList
 *      colorList
 * 测试地址: http://localhost:3000/yjx/v1/sundry/getAppBaseInfo_v1_3
 * */
function getAppBaseInfo_v1_3(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var appVer = retUtil.stringFromReq(req, 'appVer');

    // 如果有缓存, 优先使用缓存
    var uri = '/yjx/v1/sundry/getAppBaseInfo_v1_3';
    var para = {
        device : device,
        appVer : appVer
    };
    staticizeUtil.getCacheDataByRedis(uri, para, function (error, content) {
        if(content && CONFIG.DEBUG !== 1) {
            return retUtil.ret_succ_json(content, res);
        }
        sundryUtil.getAppBaseInfo_v1_3(device,appVer, function (error, result) {
            if(error) {
                console.log('yjx/v1/sundry.js [getAppBaseInfo_v1_3] error' + error);
                return retUtil.ret_err_json(error, res);
            }
            // 缓存至 redis
            staticizeUtil.recordDataByRedis(uri, para, result);
            return retUtil.ret_succ_json(result, res);
        });
    });
}
router.get('/getAppBaseInfo_v1_3', getAppBaseInfo_v1_3);
router.post('/getAppBaseInfo_v1_3', getAppBaseInfo_v1_3);


/**
 * 获取视频子分类列表_APP_1_3
 *
 * @author  毛凯
 * @date 2018-3-1
 *
 * device	是   610
 *               6 字段为 一键秀 相关标记
 *               600 一键秀网页端
 *               610 一键秀 android 端
 *               620 一键秀 iOS 端
 *               630 一键秀 嵌入网页端
 *               640 一键秀微信端
 *               650 一键秀小程序端
 * appVer  1.3.0 app版本号
 *
 * 参见接口: http://114.55.172.253:7000/project/11/interface/api/1232
 * 测试地址: http://localhost:3000/yjx/v1/sundry/getVideoAllSubtypes_v1_3
 * */
function getVideoAllSubtypes_v1_3(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var appVer = retUtil.stringFromReq(req, 'appVer');

    sundryUtil.getVideoAllSubtypes_v1_3(device,appVer,function (error, result) {
        if(error) {
            console.log('yjx/v1/sundry.js [getVideoAllSubtypes_v1_3] error' + error);
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(result,res);
    });

}
router.get('/getVideoAllSubtypes_v1_3', getVideoAllSubtypes_v1_3);
router.post('/getVideoAllSubtypes_v1_3', getVideoAllSubtypes_v1_3);


/*
* 获取作品视频关键帧时刻信息
*
* @author 王俊
* @date 2018-03-08
*
* 参数: device	    610
*                   6 字段为 一键秀 相关标记
*                   600 一键秀网页端
*                   610 一键秀 android 端
*                   620 一键秀 iOS 端
*                   630 一键秀 嵌入网页端
*                   640 一键秀微信端
*                   650 一键秀小程序端
* 参数: userId       用户唯一标识
* 参数: docId        作品唯一标识
* */
function getUserDocKeyFrameTimes_v1_3(req, res, next) {

    var device = retUtil.numberFromReq(req,"device");
    var userId = retUtil.numberFromReq(req,"userId");
    var docId = retUtil.numberFromReq(req,"docId");

    sundryUtil.getUserDocKeyFrameTimes_v1_3(device, userId, docId, function (error, times) {
        if(error) {
            console.log('yjx/v1/sundry.js [getUserDocKeyFrameTimes_v1_3] error' + error);
            return retUtil.ret_err_json(error, res);
        }
        var data = {
            keyFrameTimes : times || []
        };
        return retUtil.ret_succ_json(data, res);
    });

}
router.get('/getUserDocKeyFrameTimes_v1_3', getUserDocKeyFrameTimes_v1_3);
router.post('/getUserDocKeyFrameTimes_v1_3', getUserDocKeyFrameTimes_v1_3);


/*
 * 获取 APP 端 VIP 页面信息
 *
 * @author 毛凯
 * @date 2018-03-28
 *
 * 参数: device	    610
 *                   6 字段为 一键秀 相关标记
 *                   600 一键秀网页端
 *                   610 一键秀 android 端
 *                   620 一键秀 iOS 端
 *                   630 一键秀 嵌入网页端
 *                   640 一键秀微信端
 *                   650 一键秀小程序端
 * 参数: appver      1.4.0     APP 版本号
 * 参见接口: http://114.55.172.253:7000/project/11/interface/api/2332
 * 测试地址: http://localhost:3000/yjx/v1/sundry/getVideoAllSubtypes_v1_3?device=610&appver=1.4.0
 * */
function getAppVipInfo_v1_4(req, res, next) {

    var device = retUtil.numberFromReq(req,"device");
    var appVer = retUtil.stringFromReq(req,"appVer");

    var opt = {
        device : device,
        appVer : appVer
    };
    sundryUtil.getAppVipInfo_v1_4(opt, function (error, data) {
        if(error) {
            console.log('yjx/v1/sundry.js [getAppVipInfo_v1_4] error' + error);
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });

}
router.get('/getAppVipInfo_v1_4', getAppVipInfo_v1_4);
router.post('/getAppVipInfo_v1_4', getAppVipInfo_v1_4);


/*
* 获取视频链接对应的视频文件信息
*
* @author 王俊
* @date 2018-04-04
*
* 参数: device
* 参数: videoUrl
*
* http://127.0.0.1:3000/yjx/v1/sundry/getVideoBaseInfo_web_1_1?videoUrl=http://itbour-user.oss-cn-hangzhou.aliyuncs.com/images/U/2018/04/03/234755877_fi2bflrXCI
* */
function getVideoBaseInfo_web_1_1(req, res, next) {
    var videoUrl = retUtil.stringFromReq(req, 'videoUrl') + '';
    if(!videoUrl || videoUrl.length <=0 || videoUrl.indexOf('http') !== 0) {
        return retUtil.ret_err_json('参数错误', res);
    }
    sundryUtil.getVideoBaseInfo_web_1_1({url : videoUrl}, function (error, videoInfo) {
        if (error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(videoInfo, res);
    });
}
router.get('/getVideoBaseInfo_web_1_1', getVideoBaseInfo_web_1_1);
router.post('/getVideoBaseInfo_web_1_1', getVideoBaseInfo_web_1_1);

//------------------------------------------------------------------


/*
* 获取自由创建配置信息_ APP_3_0 添加
*
* @author 毛凯
* @date 2018-07-04
*
* 参数: device
* 参数: appVer
*
* http://127.0.0.1:3000/yjx/v1/sundry/getCustomVideoConfigure_app_v3_0
* */
function getCustomVideoConfigure_app_v3_0(req, res, next) {
    var device = retUtil.numberFromReq(req,"device");
    var appVer = retUtil.stringFromReq(req,"appVer");
    appVer = parseFloat(appVer);
    if(!device || !appVer || appVer < 2.2 ){
        return retUtil.ret_err_json("参数有误或版本过低", res);
    }
    var uri = '/yjx/v1/sundry/getCustomVideoConfigure_app_v3_0';
    var opt = {
        device : device,
        appVer : appVer
    };
    staticizeUtil.getCacheDataByRedis(uri, opt, function(getCacheErr, content){
        if (!getCacheErr && content) {
            return retUtil.ret_succ_json(content, res);
        }
        sundryUtil.getCustomVideoConfigure_app_v3_0(function (error, data) {
            if(error) {
                console.log('yjx/v1/sundry.js [getCustomVideoConfigure_app_v3_0] error' + error);
                return retUtil.ret_err_json(error, res);
            }
            var content = staticizeUtil.recordDataByRedis(uri, opt, data);
            return retUtil.ret_succ_json(content, res);
        });
    });
}
router.get('/getCustomVideoConfigure_app_v3_0', getCustomVideoConfigure_app_v3_0);
router.post('/getCustomVideoConfigure_app_v3_0', getCustomVideoConfigure_app_v3_0);




/*
* 获取音乐库列表_APP_3_0 添加
*
* @author 毛凯
* @date 2018-07-04
* 参数: device     6 字段为 一键秀 相关标记
* 参数: pageNum     页码
* 参数: pageSize    页容量
* 参数: withTotalPage  是否需要分页信息
*
* http://127.0.0.1:3000/yjx/v1/sundry/getAudioAssetsList_app_v3_0
* */
function getAudioAssetsList_app_v3_0(req, res, next) {
    var device = retUtil.numberFromReq(req,"device");
    var pageNum = retUtil.numberFromReq(req,"pageNum");
    var pageSize = retUtil.numberFromReq(req,"pageSize");
    var withTotalPage = retUtil.numberFromReq(req,"withTotalPage");
    if(!device ||  !pageSize || pageNum<0 || pageSize<=0){
        return retUtil.ret_err_json("参数有误", res);
    }
    sundryUtil.getAudioAssetsList_app_v3_0(pageNum,pageSize,withTotalPage,function (error, data) {
        if(error) {
            console.log('yjx/v1/sundry.js [getAudioAssetsList_app_v3_0] error' + error);
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getAudioAssetsList_app_v3_0', getAudioAssetsList_app_v3_0);
router.post('/getAudioAssetsList_app_v3_0', getAudioAssetsList_app_v3_0);



/*
* 获取音乐库列表_APP_3_0 添加
*
* @author 毛凯
* @date 2018-07-04
* 参数: device     6 字段为 一键秀 相关标记
* 参数: pageNum     页码
* 参数: pageSize    页容量
* 参数: withTotalPage  是否需要分页信息
*
* http://127.0.0.1:3000/yjx/v1/sundry/getAudioAssetsList_app_v3_0
* */
function getActivityWindowInfo_app_v6_6(req, res, next) {
    var device = retUtil.numberFromReq(req,"device");
    var appVer = retUtil.numberFromReq(req,"appVer");

    if(!device){
        return retUtil.ret_err_json("参数有误", res);
    }
    sundryUtil.getActivityWindowInfo_app_v6_6(function (error, data) {
        if(error) {
            console.log('yjx/v1/sundry.js [getActivityWindowInfo_app_v6_6] error' + error);
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getActivityWindowInfo_app_v6_6', getActivityWindowInfo_app_v6_6);
router.post('/getActivityWindowInfo_app_v6_6', getActivityWindowInfo_app_v6_6);

//======================================================
module.exports = router;